package com.xt;


import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
import java.awt.*;
import javax.swing.SwingUtilities;

/**
 * @author v_guiqtong
 * @version 1.0
 */
public class GUIPage {
    private JFrame frame;
    private JTextArea bigTextArea;
    private JButton startButton;

    private MouseSelectData mouseSelectData;

    public GUIPage() {
        frame = new JFrame("立减金-解放双手");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        JPanel buttonPanel = new JPanel(new GridLayout(6, 2));
        JButton button = new JButton("点击采集小程序页面");
        JTextField text = new JTextField(10);
        buttonPanel.add(button);
        buttonPanel.add(text);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    throw new RuntimeException(ex);
                }
                frame.setState(Frame.ICONIFIED);
                //开始监听鼠标操作
                String s = null;
                try {
                    s = startMouseListening(button.getText());
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                } catch (ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                }
                text.setText(s);
                frame.setState(Frame.NORMAL);
            }
        });
        JTextField token = new JTextField(10);
        buttonPanel.add(new JButton("请在右侧输入验证码token"));
        buttonPanel.add(token);
        JTextField appID = new JTextField(10);
        buttonPanel.add(new JButton("请在右侧输入百度应用appID"));
        buttonPanel.add(appID);
        JTextField appKey = new JTextField(10);
        buttonPanel.add(new JButton("请在右侧输入百度应用appKey"));
        buttonPanel.add(appKey);
        JTextField secretKey = new JTextField(10);
        buttonPanel.add(new JButton("请在右侧输入百度应用secretKey"));
        buttonPanel.add(secretKey);
        JTextField codeToken = new JTextField(10);
        buttonPanel.add(new JButton("请在右侧输入云码token"));
        buttonPanel.add(codeToken);
        bigTextArea = new JTextArea(10, 50);
        JScrollPane scrollPane = new JScrollPane(bigTextArea);

        startButton = new JButton("启动");
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.setState(Frame.ICONIFIED);
                ExcuteDemo excuteData = new ExcuteDemo();
                try {
                    excuteData.excute(bigTextArea.getText(),mouseSelectData,token.getText(),appID.getText(),appKey.getText(),secretKey.getText(),codeToken.getText());
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        frame.add(buttonPanel, BorderLayout.NORTH);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.add(startButton, BorderLayout.SOUTH);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new GUIPage();
            }
        });
    }

    /**
     * 获取选择的范围
     */
    private String startMouseListening(String name) throws IOException, ClassNotFoundException {
        HookListener hookListener = new HookListener();
        mouseSelectData = hookListener.start(hookListener, frame);
        Point resultPoint = mouseSelectData.getResultPoint();
        return String.format("起点坐标:(%s,%s),宽:%s,高:%s",resultPoint.x,resultPoint.y,mouseSelectData.getWidth(),mouseSelectData.getHeight());
    }
}
